/**
 * Package	: com.coship.tvweb.common
 * File		: BeanHelper.java
 * Company 	: Shenzhen Coship Electronics CO., LTD.
 * Version  : v1.0
 * Description 	: <Detail Description for package>
 *
 * Copyright (c) 2003-2009 Shenzhen Coship Electronics CO., LTD.
 * Rainbow Bldg.,North,Hi-Tech Industrial Park,Nanshan District,Shenzhen,China,P.C.518057.
 * All rights reserved.
 * The contents of this file are confidential and proprietary to COSHIP.
 * Copying is explicitly prohibited without the express permission of COSHIP.
 *
 * Create Date	: Oct 12, 2009
 * Create By    : 904120
 * History :$
 */
package com.coship.vasms.adstorm.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 
 * File Name : BeanHelper.java
 * 
 * @Description : spring bean获取类
 * 
 * @author 904120
 */
public final class BeanHelper {
	// private static final Logger logger = Logger.getLogger(BeanHelper.class);
	private static final String APPLICATION_FILE_NAME = "applicationContext.xml";
	private static BeanHelper instance = null;
	private static ApplicationContext applicationContext = null;

	public static BeanHelper getInstance() {
		// 双重检查
		if (instance == null) {
			synchronized (BeanHelper.class) {
				if (instance == null) {
					instance = new BeanHelper();
				}
			}
		}
		return instance;
	}

	/**
	 * 
	 * Description : 构造函数
	 * 
	 */
	private BeanHelper() {
		if (null != ServletContextKeeper.getInstance().getServletContext()) {
			applicationContext = WebApplicationContextUtils
					.getWebApplicationContext(ServletContextKeeper
							.getInstance().getServletContext());
		} else {// 单元测试
			applicationContext = new ClassPathXmlApplicationContext(
					APPLICATION_FILE_NAME);

		}
	}

	public Object getBean(String beanName) {
		return applicationContext.getBean(beanName);
	}

	public static void main(String[] args) {
		BeanHelper.getInstance().getBean("pageCrudFacade");
		System.out.println("bean inited");
	}

	/**
	 * @param applicationContext
	 *            the applicationContext to set
	 */
	protected void setApplicationContext(ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}

}
